package model

import (
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
	"log"
	"myGame/src/Config"
)

type Bullet struct {
	image       *ebiten.Image
	Width       int
	Height      int
	X           float64
	Y           float64
	Dir         int
	SpeedFactor float64
}

func NewBullet(cfg *Config.Config, realm *Realm) *Bullet {
	btImage, _, err := ebitenutil.NewImageFromFile("./src/images/bullet.png")
	if err != nil {
		log.Fatal(err)
	}
	width, height := btImage.Size()
	b := &Bullet{
		image:       btImage,
		Width:       width,
		Height:      height,
		X:           realm.X + float64(realm.Width[realm.State]),
		Y:           realm.Y[realm.State] + float64(realm.Height[realm.State]*3/10),
		Dir:         realm.Dir,
		SpeedFactor: cfg.BulletSpeedFactor,
	}
	if realm.Dir == 0 {
		b.X = realm.X
	}
	return b
}

func (bullet *Bullet) Draw(screen *ebiten.Image) {
	//设置绘制图像的选项
	op := &ebiten.DrawImageOptions{}
	//用来书写图像的作坐标生成位置
	op.GeoM.Translate(bullet.X, bullet.Y)
	//前面写好坐标，也把图像给存储到了op这个变量中了   之后就可以根句这个代码把它给画出来了
	screen.DrawImage(bullet.image, op)
}

func (bullet *Bullet) OutOfScreen(cfg *Config.Config) bool {
	return bullet.X > float64(cfg.ScreenWidth) || bullet.X < 0
}
